<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Blog\Gallery;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GalleryService
{

    use Service;

    /**
     * 图片集锦
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryIndexService( $request )
    {
        $user    = $this->getCommon()->UserCommonService();
        $with    = [
            'category' => function ( $query ) {
                $query->select('id', 'name');
            },
            'file'     => function ( $query ) {
                $query->select('id', 'url');
            },
        ];
        $where   = function ( $query ) use ( $request, $user ) {
            $query->where('static', 1);
            $query->where('user_id', $user->id);
            if ( $request->filled('name') ) $query->where('title', $request->input('name'));
            if ( $request->filled('is_show') ) $query->where('is_show', $request->input('is_show'));
            if ( $request->filled('status') ) $query->where('status', $request->input('status'));
            if ( $request->filled('root') ) $query->where('is', $request->input('is'));
            if ( $request->filled('is') ) $query->where('is', $request->input('is'));
            if ( $request->filled('category') ) $query->where('category_id', $request->input('category'));
            if ( $request->filled('created_at') ) {
                $time       = $request->input('created_at');
                $start_time = $time[ 0 ];
                $end_time   = $time[ 1 ];
                $query->whereBetween('created_at', [ $start_time, $end_time ]);
            }
        };
        $select  = [
            'id', 'user_id', 'category_id', 'title', 'is_show', 'created_at', 'updated_at', 'like', 'share',
            'browse', 'step', 'comment', 'root', 'status', 'fail', 'pic', 'is',
        ];
        $gallery = Gallery::PaginateModel($where, $with, $select, $request->input('limit'));
        return data($gallery);
    }

    /**
     * 创建
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryCreateService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BlogGalleryValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $data[ 'user_id' ]     = $user->id;
        $data[ 'title' ]       = $request->input('title');
        $data[ 'category_id' ] = $request->input('category_id');
        $data[ 'pic' ]         = $request->input('pic') ? $request->input('pic')[ 0 ] : NULL;
        $data[ 'root' ]        = $request->input('root') == TRUE ? 2 : 1;
        $data[ 'is' ]          = $request->input('is') == TRUE ? 1 : 2;
        $data[ 'content' ]     = $request->input('text');
        $data[ 'intro' ]       = $request->input('html');
        $data[ 'markdown' ]    = $request->input('markdown');
        $data[ 'status' ]      = 1;
        DB::transaction(function () use ( $request, $data ) {
            $gallery = Gallery::CreateModel($data);
            $gallery->fileAll()->sync($request->input('pic'));
        });
        return data();
    }


    /**
     * 获取详情
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryShowService( $request )
    {
        $user          = $this->getCommon()->UserCommonService();
        $with          = [
            'fileAll' => function ( $query ) {
                $query->select('id', 'url', 'filename');
            },
        ];
        $where         = function ( $query ) use ( $user, $request ) {
            $query->where('user_id', $user->id);
            $query->where('id', $request->input('id'));
        };
        $select        = [
            'id', 'user_id', 'category_id', 'title', 'root', 'is', 'markdown', 'content', 'intro',
        ];
        $gallery       = Gallery::FirstModel($where, $with, $select);
        $gallery->file = $gallery->fileAll()->pluck('id');
        return data($gallery);
    }

    /**
     * 修改
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryEditService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BlogGalleryValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $where                 = function ( $query ) use ( $request, $user ) {
            $query->where('id', $request->input('id'));
            $query->where('user_id', $user->id);
        };
        $data[ 'user_id' ]     = $user->id;
        $data[ 'title' ]       = $request->input('title');
        $data[ 'category_id' ] = $request->input('category_id');
        $data[ 'pic' ]         = $request->input('pic') ? $request->input('pic')[ 0 ] : NULL;
        $data[ 'root' ]        = $request->input('root') == TRUE ? 2 : 1;
        $data[ 'is' ]          = $request->input('is') == TRUE ? 1 : 2;
        $data[ 'content' ]     = $request->input('text');
        $data[ 'intro' ]       = $request->input('html');
        $data[ 'markdown' ]    = $request->input('markdown');
        $data[ 'status' ]      = 1;
        DB::transaction(function () use ( $request, $where, $data ) {
            $gallery = Gallery::FirstModel($where);
            $gallery->update($data);
            $gallery->fileAll()->sync($request->input('pic'));
        });
        return data();
    }

    /**
     * 修改状态
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryIsShowService( $request )
    {
        $user          = $this->getCommon()->UserCommonService();
        $where         = function ( $query ) use ( $request, $user ) {
            $query->where('id', $request->input('id'));
            $query->where('user_id', $user->id);
        };
        $key           = $request->input('key');
        $gallery       = Gallery::FirstModel($where, [], [ 'id', $key ]);
        $gallery->$key = $request->input('value') == 1 ? 1 : 2;
        $gallery->save();
        return data(NULL, $this->getErrorCode()::message(210));
    }

    /**
     * 删除图片集锦
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function GalleryDestroyService( $id )
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ( $query ) use ( $id, $user ) {
            $query->where('user_id', $user->id);
            if ( ! is_array($id) ) $query->where('id', $id);
            else $query->whereIn('id', $id);
        };
        Gallery::UpdateModel($where, [
            'static'     => 2,
            'deleted_at' => Carbon::now()->toDateTimeString(),
        ]);
        return data(NULL, $this->getErrorCode()::message(204));
    }

}
